﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using HRM.BusinessLogic;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmContractTypeList : FormBase
    {
        DataTable _dtData = null;

        public override void Show_Form()
        {
            Show();
        }


        public frmContractTypeList()
        {
            InitializeComponent();

            Init_Grid();
            //
            loadList();
            KeyPreview = true;
        }
        //Khởi tạo danh sách cho grid
        DataGridHandle _mGridHandle;
        protected void Init_Grid()
        {
            var arrColumn = new List<DataGridViewColumn>
                                {
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("STT", "RN", 30, "", true),
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Mã loại hợp đồng","ContractType_Code", 150),
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Tên Loại hợp đồng","ContractType_Name", 300),
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Tổng số tháng","ContractType_Time", 100),
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("ContractType_ID", "ContractType_ID", 0),
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("ContractType_BranchCode", "ContractType_BranchCode", 0),
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("ContractType_ProbationSalary", "ContractType_ProbationSalary", 0),
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("ContractType_Insurance", "ContractType_Insurance", 0)
                                };

            //
            _mGridHandle = FormGlobals.Grid_Init(dgvList, arrColumn, "", 1);
            _mGridHandle.Row_Validated += m_GridHandle_Row_Validated;
        }

        void m_GridHandle_Row_Validated(DataGridViewRow row)
        {
            row.Cells["RN"].Value = row.Index + 1;
        }

        private void loadList()
        {
            try
            {
                int itemCount;
                _dtData = ContractType_Controller.Instance().ContractType_GetList(out itemCount, Program.strBranchCode, 0, int.MaxValue).Tables[0];
                _dtData.Columns["ContractType_BranchCode"].DefaultValue = Program.strBranchCode;
                Globals.DataTable_RowNumber(_dtData);

                try
                {
                    Globals.DataTable_SetUnique(_dtData,"ContractType_BranchCode", "ContractType_Code", "ContractType_Name");
                }
                catch (Exception ex)
                {
                    FormGlobals.Message_Warning("Có sự trùng lặp");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            //
            dgvList.DataSource = _dtData;
        }

       
        private void frmContractTypeList_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    Dispose();
                    Close();
                    break;
               
            }
        }


        //Lưu dữ liệu
        private void btnSave_Click(object sender, EventArgs e)
        {
            // dtData.AcceptChanges();
            if (FormGlobals.Control_CheckError(dgvList))
                FormGlobals.App_ProgressShow(delegate()
                {
                    ContractType_Controller.Instance().InsertTable(_dtData);

                    //Load lại danh sách
                    loadList();
                }, "Lưu dữ liệu", "Lưu thành công");
        }
    }
}


